package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-02-15 17:18
 */
public class Q97 {
    public boolean isInterleave(String s1, String s2, String s3) {
        if(s1 == null || s2 == null || s3 == null){
            return false;
        }

        int m = s1.length();
        int n = s2.length();
        if(m + n != s3.length()){
            return false;
        }

        //代表s3的前i+j个字符是否可以用s1的前i个字符和s2的前j个字符交错形成
        boolean[][] dp = new boolean[m + 1][n + 1];

        dp[0][0] = true;
        for (int i = 0; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                if(i > 0 && s3.charAt(i + j - 1) == s1.charAt(i - 1)){
                    dp[i][j] = dp[i - 1][j];
                }

                if(j > 0 && s3.charAt(i + j - 1) == s2.charAt(j - 1)){
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }

        return dp[m][n];
    }
}
